/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

//using CustomerBikeService.Models; //added namespace references
using System.Data.Entity;


namespace Exercise1Project.Models
{
    public interface IBikeServiceRepository  // bikeService  repository interface
    {
        IEnumerable<BikeService> GetAllBikeServices();  // ADMIN - get whole table of bike services mostly for the admin searches 
        IEnumerable<BikeService> GetAllBikeServicesByBikeInShopStatus(string bikeinshopstatus); // ADMIN - return all bike services with selected bike in shop status
        IEnumerable<BikeService> GetAllBikeServicesByServiceStatus(string bikeservicestatus); // ADMIN - return all services matching the selected bike service status
        IEnumerable<BikeService> GetAllBikeServicesByInShopAndServiceStatus(string bikeservicestatus, string bikeinshopstatus); // ADMIN - return all services matching the selected bike service status plus matching bike in shop status
        IEnumerable<BikeService> GetBikeServiceByBikeID(int id); // index or list all bikeServices for a given customer bicycle  by bike id (int id is BikeId)
        void AddBikeService(BikeService bikeServiceToAdd);   // create  (add a new service to list) crud  C
        BikeService UpdateBikeService(BikeService bikeServiceToUpdate);  //update or U of crud
        void RemoveBikeService(int id);       // delete (remove a bikeservice from existing list) crud R
        BikeService GetBikeServiceByServiceId(int id);              // get a particurlar bikeservice from the list of bikeservicees
        int SaveChanges();  // save any created/ updated/ deleted changes to database table

    } // close of interface ibikeservicerepository

    public class BikeServiceRepository : IBikeServiceRepository   // implements interface
    {
        private BicycleRepairEntities1 _bs = new BicycleRepairEntities1();   // ties in with bikeservice entities which in turn ties in with bikeservice class which ties in with bikeservice table

        public IEnumerable<BikeService> GetAllBikeServices()   // returns whole bike services table used in the admin views ADMIN ALL ALL
        {
            return _bs.BikeServices;
        }

        public IEnumerable<BikeService> GetAllBikeServicesByServiceStatus(string bikeservicestatus)   // returns bike services where bikeservice status equals selected search status
        {
            return _bs.BikeServices.Where(d => d.StatusService == bikeservicestatus);
        }

        public IEnumerable<BikeService> GetAllBikeServicesByBikeInShopStatus(string bikeinshopstatus)   // returns bike services where bike in shop status equals selected search status
        {
            return _bs.BikeServices.Where(d => d.StatusBikeInShop == bikeinshopstatus);
        }

        public IEnumerable<BikeService> GetAllBikeServicesByInShopAndServiceStatus(string bikeservicestatus, string bikeinshopstatus)
        {
            return _bs.BikeServices.Where(d => d.StatusService == bikeservicestatus & d.StatusBikeInShop == bikeinshopstatus);
        }
        public IEnumerable<BikeService> GetBikeServiceByBikeID(int id)  // index or list view
        {
            return _bs.BikeServices.Where(d => d.BikeID == id);
        }

        public void AddBikeService(BikeService bikeServiceToAdd)  // create view
        {
            _bs.BikeServices.Add(bikeServiceToAdd);
            _bs.SaveChanges();
        }

        public BikeService UpdateBikeService(BikeService bikeServiceToUpdate)            // Edit action or update of crud
        {
            _bs.Entry(bikeServiceToUpdate).State = EntityState.Modified;
            _bs.SaveChanges();                                                       // save changes to database
            return bikeServiceToUpdate;
        }



        public BikeService GetBikeServiceByServiceId(int id)  // search bike table ie. bikes for BikeID matching id (EDit or crud update)
        {
            return _bs.BikeServices.FirstOrDefault(d => d.ServiceID == id);
        }

        public void RemoveBikeService(int id)  // Delete action or remove of crud
        {
            var bikeServiceToRemove = GetBikeServiceByServiceId(id);
            _bs.BikeServices.Remove(bikeServiceToRemove);
            _bs.SaveChanges();
        }


        public int SaveChanges()  // persist changes in database table used for create, update, delete
        {
            return _bs.SaveChanges();
        }


    } // close of class bikeservicerepository

} // close of namespace exercise1project models